

package cuttings;

import java.net.*;
import java.util.* ;
import java.lang.Object.*;
import java.lang.Integer;

public class PeerInfoList
{

    static LinkedList peersList = new LinkedList();
    static int condition = 0;
    static Vector<String> v = new Vector<String>();
    

    static public void processPeersInfo(String peersInfoList,String myName)
    {
        String peers[] = peersInfoList.split(";");

        int newaddition = 0;

        for (int i=0;i<peers.length;i++)
        {
            String temp[] = peers[i].split(",");
            if(temp[0].equals("adduser"))
            {
                if(temp[1].equals(myName))
                {
                    condition = 1;
                    continue;
                }
                addPeer(temp[1],temp[2],temp[3],7000);
                //add sendsocket?
                System.out.println("Add:" + temp[1]);
                newaddition = 1;
            }
            else
            {
                removePeer(temp[1]);
                //remove sendsocket?
                System.out.println("Remove:" + temp[1]);
            }
            //call vinoth update list function
            String[] st = new String[v.size()];
            //System.out.println("in stage 0");
            try {
            Menu.updateOnlinePlayers(v.toArray(st));
            }catch(Exception e) {System.out.println(e);};
        }
        //System.out.println("in stage 1");
    }

   static public boolean addPeer(String name,String hName,String hAddress,int port)
   {

        PeerInfo temp = new PeerInfo(name,hName,hAddress,port);
        boolean ret = peersList.add(temp);
        v.add(name);
        condition = 1;
        return ret;

   }

   static public boolean removePeer(String name_or_ip)
   {
        int size = peersList.size();
        int i = 0;
        String this_username = "NULL";
        int flag = 0;

        while(i<size)
        {
            PeerInfo temp = (PeerInfo) peersList.get(i);
            if (temp.name.equals(name_or_ip)||temp.hostAddress.equals(name_or_ip))
            {
                SendSocketsList.removeSendSocket(temp.hostAddress);
                peersList.remove(i);
                this_username = temp.name;
                flag = 1;
                break;
                //return true;
            }
            i++;
        }

        if(flag==1)
        {
            for(i=0;i<v.size();i++)
            {
                 if(v.get(i).equals(this_username))
                 {
                     v.remove(i);
                     return true;
                 }
            }
        }

    return false;
   }

   static public int getcondition()
   {
       return condition;
   }


   static public String getName(String userIP)
   {
        int size = peersList.size();
        int i = 0;

        while(i<size)
        {
            PeerInfo temp = (PeerInfo) peersList.get(i);
            System.out.println("in getName:"+userIP+":"+temp.hostAddress);
            if (temp.hostAddress.equals(userIP))
            {
                System.out.println("entered name");
                return (temp.name);
            }
            i++;
        }

        return ("NoName");
   }
}
